﻿
(function () {
    appModule.controller('common.views.callPolices.createOrEditModal', [
         '$scope', '$uibModalInstance', 'abp.services.app.callPolice', 'callPoliceId', 'abp.services.app.callType', 'FileUploader', 'abp.services.app.file',
        function ($scope, $uibModalInstance, callPoliceService, callPoliceId, callTypeService, fileUploader, fileService) {
            var vm = this;
            vm.saving = false;
            //首先将callPolice数据设置为null
            vm.callPolice = null;
            vm.showFile = false;
            vm.showDetail = false;

            //上传方法
            vm.uploader = new fileUploader({
                url: abp.appPath + 'Profile/UploadFile',
                headers: {
                    "X-XSRF-TOKEN": abp.security.antiForgery.getToken()
                },
                queueLimit: 1,
                autoUpload: true,
                removeAfterUpload: true,
                filters: [{
                    name: 'imageFilter',
                    fn: function (item, options) {

                        //File size check
                        if (item.size > 10485760) //10MB
                        {
                            abp.message.warn(app.localize('File_Warn_FileType'));
                            return false;
                        }

                        return true;
                    }
                }]
            });

            vm.uploader.onSuccessItem = function (fileItem, response, status, headers) {
                if (response.success) {
                    vm.fileTitle = response.result.fileName;
                    vm.fileUrl = response.result.filePath;
                    vm.callPolice.fileID = response.result.fileID;

                } else {
                    abp.message.error(response.error.message);
                }
            };

            //文件下载方法
            vm.downFile = function (filePath) {
                window.location.target = '_blank';
                window.location.href = filePath;
            }


            //触发保存方法
            vm.save = function () {
                vm.saving = true;
                callPoliceService.createOrUpdateCallPoliceAsync({ callPoliceEditDto: vm.callPolice }).then(function () {
                    abp.notify.info(app.localize('SavedSuccessfully'));
                    $uibModalInstance.close();
                }).finally(function () {
                    vm.saving = false;
                });


            };
            //取消关闭页面
            vm.cancel = function () {
                $uibModalInstance.dismiss();
            };

            //change事件
            vm.callTypeChange = function (val) {
                if (val == 1 || val == 2) {
                    vm.showFile = true;
                    vm.showDetail = false;
                }
                else {
                    vm.showFile = false;
                    vm.showDetail = true;
                }
            };

            //初始化页面
            function init() {

                //绑定一键报警分类
                callTypeService.getAllCallTypeList().then(function (result) {
                    vm.callTypeList = result.data.items;

                    callPoliceService.getCallPoliceForEditAsync({
                        id: callPoliceId
                    }).then(function (result) {
                        vm.callPolice = result.data.callPolice;

                        vm.callTypeChange(vm.callPolice.callTypeID);

                        if (vm.callPolice.fileID != 0) {
                            //绑定文件数据
                            fileService.getFileForEditAsync({
                                id: vm.callPolice.fileID
                            }).then(function (result) {
                                vm.fileTitle = result.data.file.title;
                                vm.fileUrl = result.data.file.url;
                            });
                        }
                    });
                });

            }
            //执行初始化方法
            init();
        }
    ]);

    //appModule.directive('src', function () {
    //    return {
    //        restrict: 'A',
    //        link: function (scope, element, attrs) {
    //            var current = element;
    //            scope.$watch(attrs.embedSrc, function () {
    //                var clone = element.clone().attr('src', attrs.embedSrc);
    //                current.replaceWith(clone);
    //                current = clone;
    //            });
    //        }
    //    };
    //});


})();